/*
 * Copyright (C) 2008 Peter Kling
 * 
 * This file is part of 'A Kind of Billiard', a graphical billiard simulation.
 * 
 * 'A Kind of Billiard' is free software: you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * 'A Kind of Billiard' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 'A Kind of Billiard'. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package de.akob.modeling;

import javax.media.opengl.GL;

import de.akob.mathematics.MathVector;

/**
 * Data structure for faces of a model (note: faces are considered to be convex).
 */
public class Face {
	private final Vertex[] vertices;
	private Edge[]         edges;
	private MathVector     normal = null;
	
	/**
	 * Constructs a face using the given vertices. In addition, instead of creating new edge for every connected vertex
	 * pair of the face (which will probably lead to duplicated edges if several connected faces are created), it uses
	 * an edge database. A new edge is created only, if it is not yet contained in the database.
	 * 
	 * @param edgeDB edge database
	 * @param vertices vertices that define the face (must be given counterclockwise)
	 */
	public Face(EdgeDB edgeDB, Vertex... vertices) {
		this.vertices = vertices;
		edges = new Edge[vertices.length];
		for (int i = 0; i < edges.length; i++) {
			Vertex v1 = vertices[i];
			Vertex v2 = vertices[(i + 1) % vertices.length];
			edgeDB.add(v1, v2);
			edges[i] = edgeDB.getEdge(v1, v2);
		}
	}
	
	/**
	 * Constructs a face using the given vertices.
	 * 
	 * @param vertices vertices that define the face (must be given counterclockwise)
	 */
	public Face(Vertex... vertices) {
		this.vertices = vertices;
		edges = new Edge[vertices.length];
		for (int i = 0; i < edges.length; i++)
			edges[i] = new Edge(vertices[i], vertices[(i + 1) % vertices.length]);
	}
	
	/**
	 * Draws the face normal (if present).
	 * 
	 * @param gl OpenGL object used for drawing
	 * @param length length the normal is drawn with
	 */
	public void drawNormal(GL gl, double length) {
		if (normal == null)
			return;
		MathVector meanVector = new MathVector(3);
		for (Vertex vertex : vertices)
			meanVector.add(vertex);
		meanVector.mul(1. / vertices.length);
		gl.glPushMatrix();
		gl.glTranslated(meanVector.get(1), meanVector.get(2), meanVector.get(3));
		gl.glBegin(GL.GL_LINES);
		gl.glVertex3d(0., 0., 0.);
		gl.glVertex3d(normal.get(1) * length, normal.get(2) * length, normal.get(3) * length);
		gl.glEnd();
		gl.glPopMatrix();
	}
	
	/**
	 * Returns the number of face edges.
	 * 
	 * @return number of face edges
	 */
	public int getEdgeNumber() {
		return edges.length;
	}
	
	/**
	 * Returns the edges of this face.
	 * 
	 * @return edges of this face
	 */
	public Edge[] getEdges() {
		return edges;
	}
	
	/**
	 * Returns the normal of this face.
	 * 
	 * @return face normal (null if not present)
	 */
	public MathVector getNormal() {
		return normal;
	}
	
	/**
	 * Returns the number of face vertices.
	 * 
	 * @return number of face vertices
	 */
	public int getVertexNumber() {
		return vertices.length;
	}
	
	/**
	 * Returns the vertices of this face.
	 * 
	 * @return vertices of this face
	 */
	public Vertex[] getVertices() {
		return vertices;
	}
	
	/**
	 * Executes the OpenGL operations corresponding to this primitive. Needs to be enclosed in proper
	 * <code>glBegin</code> and <code>glEnd</code> calls.
	 * 
	 * @param gl OpenGL object used for drawing
	 * @param useFaceNormal whether to use face normals instead of vertex normals (if present)
	 */
	public void glEvaluate(GL gl, boolean useFaceNormal) {
		if (normal != null && useFaceNormal) {
			gl.glNormal3dv(normal.getElements(), 0);
			for (Vertex vertex : vertices)
				vertex.glEvaluate(gl, false);
		} else {
			for (Vertex vertex : vertices)
				vertex.glEvaluate(gl, true);
		}
	}
	
	/**
	 * Sets the normal of this face. Note that the given vector will be normalized.
	 * 
	 * @param normal the face normal
	 */
	public void setNormal(MathVector normal) {
		normal.normalize();
		this.normal = normal;
	}
	
	/**
	 * Returns the number of face vertices.
	 * 
	 * @return number of face vertices
	 */
	public int vertexNumber() {
		return vertices.length;
	}
}
